package com.example.warehousems.controller;

import com.example.warehousems.component.BaseResponse;
import com.example.warehousems.component.UserInfo;
import com.example.warehousems.dao.model.Message;
import com.example.warehousems.dao.model.User;
import com.example.warehousems.service.MessageService;
import com.example.warehousems.vo.MessageIdVO;
import com.example.warehousems.vo.MessageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/message")
@Slf4j
public class MessageController {
    @Autowired
    private MessageService messageService;

    @GetMapping("/")
    BaseResponse getAll() {
        var userId = Integer.valueOf(UserInfo.get("id"));
        log.info("Get all messages for user {}", userId);
        if (!UserInfo.get("role").equals("admin"))
            return BaseResponse.success(messageService.getAll(userId));
        return BaseResponse.success(messageService.getAll());
    }

    @GetMapping("/page/{pageNum}/{pageSize}")
    BaseResponse getAllPage(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        var userId = Integer.valueOf(UserInfo.get("id"));
        log.info("Get all activities for user {}", userId);

        Pageable pageable = PageRequest.of(pageNum, pageSize);
        Page<Message> messagePage;

        if (!UserInfo.get("role").equals("admin")) {
            messagePage = messageService.getAllByUserId(userId, pageable);
        } else {
            messagePage = messageService.getAll(pageable);
        }

        return BaseResponse.success(messagePage.getContent());
    }

    @GetMapping("/top")
    BaseResponse getTop() {
        var userId = Integer.valueOf(UserInfo.get("id"));
        log.info("Get top messages for user {}", userId);
        return BaseResponse.success(messageService.getTop(userId));
    }

    @PostMapping("/")
    BaseResponse addMessage(@RequestBody MessageVO messageVO) {
        var userId = Integer.valueOf(UserInfo.get("id"));
        messageService.addMessage(userId, messageVO.getContent(), 0);
        return BaseResponse.success("Message sent");
    }

    @PostMapping("/read/{id}")
    BaseResponse markAsRead(@PathVariable Integer id) {
        var userId = Integer.valueOf(UserInfo.get("id"));
        log.info("Mark message {} as read for user {}", id, userId);
        var res = messageService.markAsRead(id, userId);
        if (res == -1) {
            return BaseResponse.error("Permission denied");
        }
        return BaseResponse.success("ok");
    }

    @PostMapping("/read")
    BaseResponse markAllAsRead(@RequestBody MessageIdVO ids) {
        log.info(ids.toString());
        var userId = Integer.valueOf(UserInfo.get("id"));
        log.info("Mark all messages as read for user {}", userId);
        messageService.markAllAsRead(userId, ids.getIds());
        return BaseResponse.success("ok");
    }
}
